package avicit.bdp.dcs.common.rest;

import avicit.bdp.dcs.utils.VersionPropertyUtil;
import avicit.platform6.core.rest.msg.ResponseMsg;
import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2022-10-29 11:55
 * @类说明：
 * @修改记录：
 */
@RestController
@RequestMapping("/api/bdp/dcs")
@Api(tags = "接口")
public class VersionRest {

    private static final Logger logger = LoggerFactory.getLogger(VersionRest.class);

    @GetMapping("/help")
    @ApiOperation(value = "查看版本信息")
    public ResponseMsg<JSONObject> getVersion() {
        ResponseMsg<JSONObject> responseMsg = new ResponseMsg<>();
        try {
            String gitBuildTime = VersionPropertyUtil.getString("git.build.time");
            String gitCommitId = VersionPropertyUtil.getString("git.commit.id");

            if (StringUtils.isNotEmpty(gitBuildTime)) {
                if(gitBuildTime.contains("+")){
                    gitBuildTime = gitBuildTime.substring(0, gitBuildTime.indexOf("+"));
                }
            }

            JSONObject json = new JSONObject();
            json.put("gitBuildTime", gitBuildTime);
            json.put("gitCommitId", gitCommitId);
            responseMsg.setResponseBody(json);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            responseMsg.setRetCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
            responseMsg.setErrorDesc(e.getMessage());
        }
        return responseMsg;
    }

}
